<!DOCTYPE html>
<html>
{include file="public/header" /}
<body>
<style>
    .layui-input-inline {
        width: 400px !important;
    }
    .quantity input {
        width: 25%;
        padding: 5px 10px;
        text-align: center;
    }
</style>

<div class="layui-fluid" id="app">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-sm7" style="border-right: 5px solid #f4f4f4">
                    <div class="layui-col-md12">
                        <form class="layui-form " action="">
                            <div class="layui-form-item">
                                <span style="float: left;width: 85%">
                                <input type="text" name="keyword" value="" class="layui-input" autocomplete="off"
                                       placeholder="商品名称">
                                </span>
                                <span style="float: left;width: 15%">
                                            <button class="layui-btn" lay-submit lay-filter="select" style="width: 100%"><i class="layui-icon">&#xe615;</i>搜索</button>
                                </span>
                            </div>
                        </form>
                    </div>
                    <div class="layui-col-md12">

                        <table class="layui-table" lay-skin="line">
                            <thead>
                            <tr>
                                <th>商品名</th>
                                <th>型号</th>
                                <th>材质</th>
                                <th>位置</th>
                                <th>品牌</th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="(item,index) in items" :data_id="item.id">
                                <td onmouseover="name(this)"><b>{{item.name}}</b></td>
                                <td>{{item.xinghao}}</td>
                                <td>{{item.caizhi}}</td>
                                <td>{{item.weizhi}}</td>
                                <td>{{item.pinpai}}</td>
                                <td>
                                    <button class="layui-btn layui-btn-sm layui-btn-primary"
                                            @click="selectedGood(item)">
                                        选择
                                    </button>

                                </td>
                            </tr>

                            </tbody>
                        </table>

                    </div>
                    <div id="demo7"></div>
                </div>
                <div class="layui-col-sm4">
                    <!--<div class="layui-col-md12" style="height: 38px;margin-bottom:15px;line-height: 38px;padding-left: 15px;font-size: 20px">-->
                        <!--材料清单-->
                    <!--</div>-->
                    <h3>材料清单</h3>
                    <hr>
                    <div class="layui-col-md12" style="height: 350px;overflow:auto">

                        <table class="layui-table" lay-skin="line">
                            <thead>
                            <tr>
                                <th>商品名</th>
                                <th>数量</th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="(item,index) in list" :data_id="item.id">
                                <td><b>{{item.name}}</b></td>
                                <td>
                                    <div class="quantity">
                                        <a href="javascript:void 0" @click="change(item,-1)"style="margin-right:5%">-</a>
                                        <input type="text" value=""  v-model="item.num">
                                        <a href="javascript:void 0" @click="change(item,1)" style="margin-left: 5%">+</a>
                                    </div>
                                </td>
                                <td>
                                    <button class="layui-btn layui-btn-sm layui-btn-primary"
                                            @click="delGood(index)">
                                        删除
                                    </button>
                                </td>
                            </tr>

                            </tbody>
                        </table>

                    </div>
                    </form>
                </div>
            </div>
        </div>
        <div style="text-align: center;position: fixed;bottom: 0px;background-color: #ffffff;width: 100%"  >
            <button class="layui-btn " onclick="sure()" style="width: 10%;margin: 10px">
                确认
            </button>
            <button class="layui-btn layui-bg-gray" onclick="cancel()" style="width: 10%;margin: 10px">
                取消
            </button>
        </div>
    </div>


</div>
</div>

<script>

    var app = new Vue({
        el: '#app',
        data: {
            items: [],
            list:[],
            curr: 1,//当前页
            limit: 6,//当前页数据个数

        },
        methods: {
            //获取所有数据
            selectedGood: function (n) {
                var i=0;
                this.list.forEach(function (item,index) {
                    if(item.id==n.id){
                       i=1;
                    }
                });
                if(i==0){
                    layer.msg('选择成功！');
                    Vue.set(n,'num','1');
                    this.list.push(n);
                }
                else{
                    layer.msg('请不要重复选择哟！');
                }
            },
            delGood: function (index) {
                this.list.splice(index,1);
            },
            change:function (item,way) {
                if(way>0){
                   item.num++;
                }
                else {
                    item.num--;
                    if(item.num<1){
                        item.num=1;
                    }
                }
            },
        },
    });

    function init() {
        layer.load(2);
        $.get("{:url('select_material')}" , {page: app.curr, limit: app.limit},
            function (res) {
                layer.closeAll();
                app.items = res.data;
            }
        );
    }

    $(function () {
        if(parent.app.cailiao=="已选择") {
            var localGood = layui.data('good');
            var list = localGood.goodList;
            if (list) {
                app.list = list;
            }
        }

    });

    layui.use(['table','laypage','form'], function () {
        var $ = layui.$
            , layer = layui.layer
            , form = layui.form;
        var laypage = layui.laypage
        var table = layui.table;
        laypage.render({
            elem: 'demo7'
            ,count: "{$count}" //数据总数
            ,curr:1
            ,limit:6
            , limits: [6,12]
            , layout: ['count', 'prev', 'page', 'next']
            , jump: function (obj) {
                console.log(obj)
                app.curr = obj.curr;
                app.limit = obj.limit;
                init();
            }
        });

        form.on('submit(select)', function (data) {
            if (!data.field.keyword) {
                return false;
            }

            layer.load(2);
            $.get("{:url('select_good')}",{keyword: data.field.keyword},
                function (res) {
                    layui.use(['laypage'], function () {
                        var laypage = layui.laypage;
                        //完整功能
                        laypage.render({
                            elem: 'demo7'
                            ,curr:1
                            ,limit:6
                            , count: res.total
                            , limits: [6, 12]
                            , layout: ['count', 'prev', 'page', 'next']
                            , jump: function (obj) {
                                console.log(obj)
                                app.curr = obj.curr;
                                app.limit = obj.limit;
                                layer.load(2);

                                $.get("{:url('select_good')}", { page: app.curr,limit: app.limit,keyword: data.field.keyword},
                                    function (res) {
                                        layer.closeAll();
                                        app.items = res.data;
                                    }
                                );
                            }
                        });

                    });
                }
            );

            return false;
        });

    });
    function sure() {
        layui.data('good', {
            key: 'goodList',
            value: app.list
        });
        var localGood = layui.data('good');
        if(localGood.goodList.length!=0){
            console.log("fddwqdq");
            parent.app.cailiao="已选择";
            parent.layer.closeAll();

        }
        else{
            parent.app.cailiao="选择";
            parent.layer.closeAll();
        }
    }
    function name(that) {
        var data = $(that).text();
        layer.tips(data, $(that), {
            tips: [1, '#009688'],
            time: 4000
        });
    }
    function cancel() {
        parent.layer.closeAll();
    }

</script>
</body>
</html>